$(document).ready(function() {
    // jQuery is properly loaded at this point
    // so proceed to bind the Cordova's deviceready event
    //app.readPosts();
    $(document).bind('deviceready', app.onDeviceReady);
});

var app = {
    posts_url: "http://127.0.0.1:8888/cakeStrap-example-master/rest_posts",
    onDeviceReady: function() {
        console.log('Device is ready');
        app.readPosts();
    },
    readPosts: function() {
        console.log('Reading posts');
        $.ajax({
            type: "GET",
            dataType: "json",
            url: app.posts_url,
            success: app.onSuccess,
            error: app.onError
        });
        console.log('Reading posts asynchrounously');
    },
    onSuccess: function(data) {
        var items = [];
        $.each(data, function(key, val) {
            items.push('<a href="' + app.posts_url + val.id + '">' + val.id + ' - ' + val.title + '</a>');
        });
        $('#posts').html(items.join('<br/>'));
        console.log('Exiting onSuccess');
    },
    onError: function(data, textStatus, errorThrown) {
        console.log('Data: ' + data);
        console.log('Status: ' + textStatus);
        console.log('Error: ' + errorThrown);
        $("#posts").html('Error while loading posts');
        console.log('Exiting onError');
    }
};
